#! /bin/sh -e
#
# librsync -- the library for network deltas
# Copyright (C) 2001, 2014 by Martin Pool <mbp@sourcefrog.net>
#
# largefile.test: Generate some large random files with 50% matches
# and generate signature, delta, and patch files, comparing for
# correctness.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation; either version 2.1 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

# Note this test is not included in make check because it creates very
# large data files and takes a long time.

srcdir='.'
. $srcdir/testcommon.sh

# Note $1 is used to specify "RDIFF" by cmake tests, so we use
# arguments after that.

# Allow the number of blocks to be specified in $2 in the form '64K'.
blocks=${2:-64K}

# Allow a data directory to be specified in $3 to use persistent
# random data files to make tests more repeatable, otherwise use
# $tmpdir.
datadir=${3:-$tmpdir}
echo "DATADIR $datadir"

# Use $4 to optionally specify a block size to use.
blocksize=${4:-1024}

old="$datadir/old.$blocks"
new="$datadir/new.$blocks"
sig="$datadir/sig.$blocks"
delta="$datadir/delta.$blocks"
out="$datadir/out.$blocks"

if [ ! -f "$old" ]; then
   mkdir -p $datadir
   dd bs=$blocks count=1024 if=/dev/urandom >"$old"
   dd bs=$blocks count=256 if=/dev/urandom >"$new"
   dd bs=$blocks count=256 skip=128 if="$old" >>"$new"
   dd bs=$blocks count=256 if=/dev/urandom >>"$new"
   dd bs=$blocks count=256 skip=640 if="$old" >>"$new"
fi

run_test time ${RDIFF} $debug -f -s -b $blocksize -S 8 signature $old $sig
run_test time ${RDIFF} $debug -f -s delta $sig $new $delta
run_test time ${RDIFF} $debug -f -s patch $old $delta $out
check_compare $new $out "large files"
true
